package com.tudulist.aspect;

import javax.inject.Inject;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import com.tudulist.model.TodoList;
import com.tudulist.notifier.ConsoleNotifier;
import com.tudulist.notifier.StringMessage;

@Aspect
@Component
public class NotifierAdvice {

	@Inject
	ConsoleNotifier notifier;

	@AfterReturning(pointcut = "execution(* com.tudulist.service.rest.TodoListResourceServer.createTodoList(..))", returning = "mergedTodoList")
	public void handleNotification(JoinPoint jp, TodoList mergedTodoList) {

		notifier.notify(new StringMessage("Call of " + jp.getSignature().getName()));

		if (mergedTodoList != null) {
			notifier.notify(new StringMessage("Todo list '" + mergedTodoList.getName() + "' created with sucess"));
		} else {
			notifier.notify(new StringMessage("Error merging todolist!"));
		}
	}

}
